/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ConnectDB;

import java.sql.*;
import javax.sql.*;
/**
 *
 * @author THIENSUHACK
 */
public class connectData {
    public Connection con;
    public String connURL="";
    public connectData()
    {
        connURL="jdbc:odbc:abc,";
    }
    public  String createConn()
    {        
        try
        {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DKTinChi;","sa","123");
        return "OK";
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            con=null;
            return "NOT";
        }catch(SQLException sqle){
            sqle.printStackTrace();
            con=null;
             return "NOT";
        }
       
    }
    public String GetDataDB()
    {
        try {
            String str="Select * from SINHVIEN";
            
            createConn();
            
            if(con!=null){
               Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                       ResultSet.CONCUR_READ_ONLY);
               ResultSet rs=st.executeQuery(str);
               String temp="";
               while(rs.next())
               {
                   String id=rs.getString("MASV");
                   String name=rs.getString("HOTEN");
                   temp+=id+name;
               }
               st.close();
               return temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "can't connect to DB";
        }
        return "can't connect to DB";
    }
    public ResultSet Select_GetDataDB(String query)
    {
        ResultSet resultset;
        
        try {           
            
            createConn();            
            if(con!=null){
               Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                       ResultSet.CONCUR_READ_ONLY);
               
               resultset=st.executeQuery(query);
               
               return resultset;
            }
        } catch (Exception e) {          
            return null;
        }
        
        return null;
    }
    public int Insert_Update_Delete_DataDB(String query)
    {       
     int kq=0;   
        try {           
            
            createConn();
            
            if(con!=null){
               Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                       ResultSet.CONCUR_READ_ONLY);
               kq=st.executeUpdate(query);
               
               return kq;
            }
        } catch (Exception e) {            
            return 0;
        }
        
        return 1;
    }
    
}
